package com.woniu.controller;


import com.woniu.Result;
import com.woniu.pojo.Comment;
import com.woniu.service.ICommentService;
import com.woniu.utils.SensitivewordFilter;
import com.woniu.vo.CommentVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

import static net.sf.jsqlparser.parser.feature.Feature.set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yellow Docker
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
    private SensitivewordFilter sensitivewordFilter;
    @Autowired
    private ICommentService commentService;

    @ApiOperation("通过服务id查询服务评价")
    @GetMapping("/service/{id}")
    public Result getServiceComment(@PathVariable Integer id){
        List<Comment> comments = commentService.getServiceComment(id);
        return Result.ok(comments);
    }

    @ApiOperation("通过门店id查询门店评价")
    @GetMapping("/shop/{id}")
    public Result getShopComment(@PathVariable Integer id){
        List<Comment> comments = commentService.getShopComment(id);
        return Result.ok(comments);
    }

    @ApiOperation("查询门店的评分")
    @GetMapping("/shop")
    public Result getShopAvgComment(Date date){
        List<CommentVo> commentVos = commentService.getShopAvgComment(date);
        return Result.ok(commentVos);
    }

    @ApiOperation("新增评论")
    @PostMapping
    public Result add(@RequestBody Comment comment){
        Set<String> sensitiveWord = sensitivewordFilter.getSensitiveWord(comment.getContent(), 1);
        if (sensitiveWord.size()>0){
            log.info("语句中包含敏感词的个数为：" + sensitiveWord.size() + "。包含：" + sensitiveWord);
            String newComment = sensitivewordFilter.replaceSensitiveWord(comment.getContent(), 1, "*");
            return Result.fail("语句中包含敏感，请重新输入");
        }
        return Result.ok(comment.getContent());
    }

}

